package cn.tedu.egweibo4.controller;

import cn.tedu.egweibo4.base.response.JsonResult;
import cn.tedu.egweibo4.base.response.StatusCode;
import cn.tedu.egweibo4.mapper.WeiboMapper;
import cn.tedu.egweibo4.pojo.dto.WeiboDTO;
import cn.tedu.egweibo4.pojo.entity.Weibo;
import cn.tedu.egweibo4.pojo.vo.UserVO;
import cn.tedu.egweibo4.pojo.vo.WeiboDetailVO;
import cn.tedu.egweibo4.pojo.vo.WeiboIndexVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@Api(tags = "02.微博模块")
@RequestMapping("/v1/weibo/")
public class WeiboController {

    @Autowired
    private WeiboMapper weiboMapper;

    @PostMapping("insert")
    @ApiOperation(value = "发布微博功能")
    public JsonResult insert(@RequestBody WeiboDTO weiboDTO, @ApiIgnore HttpSession httpSession){
        log.debug("weiboDTO = {}",weiboDTO);
        UserVO userVO = (UserVO) httpSession.getAttribute("user");
        if (userVO == null){
            return new JsonResult(StatusCode.NOT_LOGIN);
        }
        Weibo weibo = new Weibo();
        BeanUtils.copyProperties(weiboDTO,weibo);
        weibo.setCreated(new Date());
        weibo.setUserId(userVO.getId());
        weiboMapper.insert(weibo);

        return JsonResult.ok();
    }

    @GetMapping("selectIndex")
    @ApiOperation(value = "微博首页列表功能")
    public JsonResult selectIndex(){
        List<WeiboIndexVO> weiboIndexVOList = weiboMapper.selectIndex();
        return JsonResult.ok(weiboIndexVOList);
    }

    @GetMapping("selectById")
    @ApiOperation(value = "微博详情页功能")
    @ApiImplicitParam(value = "微博编号" , required = true,dataType = "long")
    public JsonResult selectById(Long id){
        WeiboDetailVO weiboDetailVO = weiboMapper.selectById(id);
        return JsonResult.ok(weiboDetailVO);
    }

}
